{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21cfea06-af98-496a-b13b-106c335a2e65",
   "metadata": {},
   "source": [
    "# Understand Number Space - Similar Mode\n",
    "\n",
    "Our `NumberSpace` has a SIMILAR mode too, enabling search for similar numbers. \n",
    "\n",
    "Check MinMax mode [here](number_embedding_minmax.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bf67328-efe5-4c88-9c36-ce2d4b20d89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==37.5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11664035-fff3-4c38-97f3-f2fbb0d46778",
   "metadata": {},
   "outputs": [],
   "source": [
    "from superlinked import framework as sl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3628c066-5491-40ff-867a-f3f7dec6f8ae",
   "metadata": {},
   "source": [
    "## Similar mode\n",
    "\n",
    "In this case we prefer numbers that are similar to the number we are going to query with."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2df24eaf-f9b8-404b-8b7f-0a9d2c2284df",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person(sl.Schema):\n",
    "    id: sl.IdField\n",
    "    height: sl.Integer  # set height in cms for example\n",
    "\n",
    "\n",
    "person = Person()\n",
    "\n",
    "height_space = sl.NumberSpace(\n",
    "    number=person.height, min_value=100, max_value=220, mode=sl.Mode.SIMILAR\n",
    ")  # height out of this range is considered an outlier in our use case\n",
    "person_index = sl.Index(height_space)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4aed826a-470e-4e53-8f2a-e8e529373b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "source: sl.InMemorySource = sl.InMemorySource(person)\n",
    "executor = sl.InMemoryExecutor(sources=[source], indices=[person_index])\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "61d76e31-72f4-4404-8cc1-31c08d08d978",
   "metadata": {},
   "outputs": [],
   "source": [
    "source.put([{\"id\": \"person1\", \"height\": 175}, {\"id\": \"person2\", \"height\": 180}])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "198f0bce-5d7c-412b-a989-d0ab60982770",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = sl.Query(person_index).find(person).similar(height_space, 178).select_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aaa75750-3b1c-4d68-af9c-133a249f6b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>180</td>\n",
       "      <td>person2</td>\n",
       "      <td>0.999657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175</td>\n",
       "      <td>person1</td>\n",
       "      <td>0.999229</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height       id  similarity_score\n",
       "0     180  person2          0.999657\n",
       "1     175  person1          0.999229"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = app.query(query)\n",
    "sl.PandasConverter.to_pandas(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74a28f44-ea87-40a6-a385-a4b218f8bee1",
   "metadata": {},
   "source": [
    "As expected, the person 180 cm high comes first (as their height is closer to the queried number, 178), and the person with height of 175 cm comes second - while the difference in score is extremely low."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "superlinked-py3.10",
   "language": "python",
   "name": "superlinked-py3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
